﻿
Partial Public MustInherit Class autoJob(Of hash_t, impl_t As autoJob(Of hash_t, impl_t))
    Inherits basicJob(Of impl_t, hash_t)

    Protected Shared ReadOnly impl_type As Type = Nothing

    Shared Sub New()
        impl_type = GetType(impl_t)
    End Sub

    Protected NotOverridable Overrides Function job_valid() As Boolean
        Return MyBase.job_valid() AndAlso auto_valid()
    End Function

    Private Function auto_valid() As Boolean
        For i As Int64 = 0 To field.required.Length() - 1
            If required(i) And Not valid(i) Then
                Return False
            End If
        Next

        For i As Int64 = 0 To field.init.Length() - 1
            If respond(i) Then
                init(i)
            End If
        Next

        Return True
    End Function

    Protected NotOverridable Overrides Sub receive_lop()
        MyBase.receive_lop()
        For i As Int64 = 0 To field.required.Length() - 1
            If Not required(i) Then
                set_value(i, Nothing)
            End If
        Next
    End Sub

    Protected NotOverridable Overrides Sub respond_lop()
        MyBase.respond_lop()
        For i As Int64 = 0 To field.respond.Length() - 1
            If Not respond(i) Then
                set_value(i, Nothing)
            End If
        Next
    End Sub

    Protected NotOverridable Overrides Sub copy(ByVal i As impl_t)
        For j As Int64 = 0 To field.fields.Length() - 1
            set_value(j, i.get_value(j))
        Next
    End Sub

    Protected Overrides Function paras() As toBytes()
        Dim rtn() As equalBytes = Nothing
        ReDim rtn(field.fields.Length() - 1)
        For i As Int64 = 0 To field.fields.Length() - 1
            rtn(i) = field.serializers(i)(get_value(i))
        Next
        Return rtn
    End Function

    Protected Overrides Sub frombytes()
        For i As Int64 = 0 To field.deserializers.Length() - 1
            Dim p As fromBytes = Nothing
            p = next_para()
            set_value(i, field.deserializers(i)(p))
        Next
    End Sub

    Protected Overrides Function types() As Type()
        Return field.types
    End Function
End Class
